package com.xjtu.mybatisplus.web.controller;

import com.xjtu.mybatisplus.common.annotation.PageView;
import com.xjtu.mybatisplus.common.properties.JwtProperties;
import com.xjtu.mybatisplus.common.result.PageResult;
import com.xjtu.mybatisplus.common.utls.JwtUtil;
import com.xjtu.mybatisplus.common.utls.RedisUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.xjtu.mybatisplus.common.JsonResponse;
import com.xjtu.mybatisplus.service.ArticleService;
import com.xjtu.mybatisplus.model.domain.Article;

import java.util.List;


/**
 *
 *  前端控制器
 *
 *
 * @author lsong
 * @since 2023-12-27
 * @version v1.0
 */
@Controller
@RequestMapping("/api/article")
public class ArticleController {

    private final Logger logger = LoggerFactory.getLogger( ArticleController.class );

    @Autowired
    private ArticleService articleService;

    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 描述：返回采编人员的所有文章
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse list(@RequestHeader(name = "token", required = false) String token) {
        Long userId = JwtUtil.getUserId(token);

        List<Article> articles = articleService.listByUserId(userId);
        return JsonResponse.success(articles);
    }

    @GetMapping("/auditList")
    @ResponseBody
    public JsonResponse auditList(@RequestHeader(name = "token", required = false) String token) {
        Long userId = JwtUtil.getUserId(token);

        List<Article> articles = articleService.auditList(userId);
        return JsonResponse.success(articles);
    }

    /**
    * 描述：根据Id 查询
    *
    */
    @PageView
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        Article  article =  articleService.getById(id);
        String key = "articleId_" + article.getId();
        Long watch = redisUtil.size(key) + article.getWatch();
        article.setWatch(Math.toIntExact(watch));
        return JsonResponse.success(article);
    }
    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        articleService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateArticle(Article  article) throws Exception {
        articleService.updateById(article);
        return JsonResponse.success(null);
    }


    /**
    * 描述:创建Article
    *
    */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(@RequestHeader(name = "token", required = false) String token, @RequestBody Article  article) throws Exception {
        System.out.println(article);
        if (token == null) {
            return JsonResponse.failure("请先登录");
        }

        Long userId = JwtUtil.getUserId(token);
        article.setUserId(userId);

        articleService.saveArticle(article);
        return JsonResponse.success(null);
    }

    @PutMapping("/audit/{id}")
    @ResponseBody
    public JsonResponse audit(@PathVariable("id") Long id) {
        articleService.audit(id);

        return JsonResponse.success(null);
    }

    @PutMapping("/withdraw/{id}")
    @ResponseBody
    public JsonResponse withdraw(@PathVariable("id") Long id) {
        articleService.withdraw(id);

        return JsonResponse.success(null);
    }

    @GetMapping("/newest")
    @ResponseBody
    public JsonResponse getNewestArtical(@Param("page")Integer page,@Param("limit") Integer size){
        PageResult articleList = articleService.getNewestArtical(page,size);
        return JsonResponse.success(articleList);
    }

    @GetMapping("/cate")
    @ResponseBody
    public JsonResponse getArticalByCate(@Param("page")Integer page,@Param("limit") Integer size,@Param("cate") Integer cate){
        PageResult articleList = articleService.getArticalByCate(page,size,cate);
        return JsonResponse.success(articleList);
    }

    @GetMapping("/hot")
    @ResponseBody
    public JsonResponse getHotArtical(){
        List<Article> articleList = articleService.getHotArtical();
        return JsonResponse.success(articleList);
    }

//    @GetMapping("sum")
//    @ResponseBody
//    public  JsonResponse getTotal() {
//
//    }

}

